package com.code.question1.controller;

import com.code.question1.base.SearchType;
import com.code.question1.impl.Student;
import com.code.question1.impl.StudentAgeException;
import com.code.question1.impl.StudentNoException;

import java.io.*;
import java.util.List;

public class StudentController {
    /**
     * 初始化学生
     */
    public void InitStudents() throws StudentAgeException, IOException, ClassNotFoundException {
        File file = new File("");
        String projectPath = file.getCanonicalPath();
        String diskPath = projectPath.substring(0, projectPath.indexOf("\\") + 1);
        File studentFile = new File(diskPath + "student.txt");
        if (!studentFile.exists()) {
            Student student = new Student("2020100301", "张飞", "30");
            student.Post(student);
            student = new Student("2020100302", "关羽", "35");
            student.Post(student);
            student = new Student("2020100303", "马超", "38");
            student.Post(student);
            student = new Student("2020100304", "赵云", "33");
            student.Post(student);
            student = new Student("2020100305", "黄忠", "50");
            student.Post(student);
        } else {
            //反序列化对象信息
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(studentFile));
            List<Student> students = (List<Student>) ois.readObject();
            Student student = new Student();
            for (Student stu : students) {
                student.Post(stu);
            }
            ois.close();
        }
    }

    /**
     * 添加学生
     */
    public void AddStudent() throws IOException, StudentNoException, StudentAgeException {
        Student addStudent = new Student();
        String studentNo = "", studentName = "";
        System.out.println("请输入学生学号：");
        studentNo = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        addStudent.setStudentNo(studentNo);
        System.out.println("请输入学生姓名：");
        studentName = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        addStudent.setName(studentName);
        System.out.println("请输入学生年龄：");
        String inputAge = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        addStudent.setAge(inputAge);
        addStudent.Post(addStudent);
        System.out.println("添加成功！！");
        System.out.println(addStudent.toString());
    }

    /**
     * 修改学生信息
     *
     * @throws IOException
     */
    public void UpdateStudent() throws IOException, StudentNoException, StudentAgeException {
        //学号校验正则
        String reg = "[1-9]\\d{9}";
        String regAge = "[1-9]\\d{1}";
        String studentNo = "", studentName = "";
        System.out.println("请输入学生学号：");
        studentNo = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        if (!Student.ExistsStudent(studentNo)) {
            System.out.println("该学生不存在！");
            return;
        }
        Student updateStudent = new Student();
        updateStudent.setStudentNo(studentNo);
        System.out.println("请输入修改的学生姓名：");
        studentName = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        updateStudent.setName(studentName);
        System.out.println("请输入修改的学生年龄：");
        String inputAge = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        updateStudent.setAge(inputAge);
        updateStudent.Put(updateStudent);
        System.out.println("更新成功！！");
        System.out.println(updateStudent.toString());
    }

    /**
     * 查找学生信息
     */
    public void FindStudents() throws IOException {
        System.out.println("请输入要查找学生的信息类别：");
        System.out.println("1:根据学号查找");
        System.out.println("2:根据姓名查找");
        System.out.println("3:根据年龄查找");
        String input = (new BufferedReader(new InputStreamReader(System.in))).readLine();
        Student student = new Student();
        switch (input) {
            case "1":
                System.out.println("请输入学号：");
                String studentNo = (new BufferedReader(new InputStreamReader(System.in))).readLine();
                System.out.println(student.toString(student.Get(studentNo, SearchType.No)));
                break;
            case "2":
                System.out.println("请输入姓名：");
                String studentName = (new BufferedReader(new InputStreamReader(System.in))).readLine();
                System.out.println(student.toString(student.Get(studentName, SearchType.Name)));
                break;
            case "3":
                System.out.println("请输入年龄：");
                String age = (new BufferedReader(new InputStreamReader(System.in))).readLine();
                System.out.println(student.toString(student.Get(age, SearchType.Age)));
                break;
            default:
                System.out.println(student.toString());
                break;
        }
    }

    /**
     * 删除学生信息
     *
     * @throws IOException
     */
    public void DeleteStudent() throws IOException {
        //学号校验正则
        String reg = "[1-9]\\d{9}";
        String studentNo = "";
        System.out.println("请输入学生学号：");
        while (true) {
            studentNo = (new BufferedReader(new InputStreamReader(System.in))).readLine();
            if (studentNo.matches(reg)) {
                if (Student.ExistsStudent(studentNo)) {
                    break;
                } else {
                    System.out.println("该学生不存在！！");
                    return;
                }
            } else
                System.out.println("学号格式有误，请重新输入：");
        }
        Student deleteStudent = new Student();
        deleteStudent.Delete(studentNo);
        System.out.println("删除成功！！");
        System.out.println(deleteStudent.toString());
    }

    /**
     * 登出
     *
     * @param student
     * @throws IOException
     */
    public void Logout(Student student) throws IOException {
        //退出序列化学生信息 持久化处理
        File file = new File("");
        String projectPath = file.getCanonicalPath();
        String diskPath = projectPath.substring(0, projectPath.indexOf("\\") + 1);
        File studentFile = new File(diskPath + "student.txt");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(studentFile));
        oos.writeObject(student.Get());
        oos.close();
    }
}
